/**
 * @file softplusv2_custom.cpp
 *
 * Copyright (C) 2024. Huawei Technologies Co., Ltd. All rights reserved.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
#include "kernel_operator.h"

constexpr int32_t TOTAL_LENGTH = 8 * 2048;
constexpr int32_t USE_CORE_NUM = 8;
constexpr int32_t BLOCK_LENGTH = TOTAL_LENGTH / USE_CORE_NUM;
constexpr int32_t TILE_NUM = 8;
constexpr int32_t BUFFER_NUM = 2;
constexpr int32_t TILE_LENGTH = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM;

class KernelLogSigmoid {
public:
  __aicore__ inline KernelLogSigmoid() {}

  __aicore__ inline void Init(GM_ADDR x, GM_ADDR z) {
    xGm.SetGlobalBuffer(
        (__gm__ half *)x + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
    zGm.SetGlobalBuffer(
        (__gm__ half *)z + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
    pipe.InitBuffer(inQueueX, BUFFER_NUM, TILE_LENGTH * sizeof(half));
    pipe.InitBuffer(outQueueZ, BUFFER_NUM, TILE_LENGTH * sizeof(half));
  }

  __aicore__ inline void Process() {
    int32_t loopCount = TILE_NUM * BUFFER_NUM;
    for (int32_t i = 0; i < loopCount; ++i) {
      CopyIn(i);
      Compute(i);
      CopyOut(i);
    }
  }

private:
  __aicore__ inline void CopyIn(int32_t progress) {
    AscendC::LocalTensor<half> xLocal = inQueueX.AllocTensor<half>();
    AscendC::DataCopy(xLocal, xGm[progress * TILE_LENGTH], TILE_LENGTH);
    inQueueX.EnQue(xLocal);
  }

  __aicore__ inline void Compute(int32_t progress) {
    AscendC::LocalTensor<half> xLocal = inQueueX.DeQue<half>();
    AscendC::LocalTensor<half> yLocal = outQueueZ.AllocTensor<half>();
    AscendC::LocalTensor<half> tmpLocal = inQueueX.AllocTensor<half>();

    // Step 1: tmp = -x
    for (int i = 0; i < TILE_LENGTH; ++i) {
      tmpLocal.SetValue(i, static_cast<half>(0.0f));
    }
    AscendC::Sub(tmpLocal, tmpLocal, xLocal, TILE_LENGTH);

    // Step 2: tmp = exp(tmp) = exp(-x)
    AscendC::Exp(tmpLocal, tmpLocal, TILE_LENGTH);

    // Step 3: tmp = 1 + tmp = 1 + exp(-x)
    for (int i = 0; i < TILE_LENGTH; ++i) {
      yLocal.SetValue(i, static_cast<half>(1.0f));
    }
    AscendC::Add(tmpLocal, tmpLocal, yLocal, TILE_LENGTH);

    // Step 4: tmp = ln(tmp) = ln(1 + exp(-x))
    AscendC::Ln(tmpLocal, tmpLocal, TILE_LENGTH);

    // Step 5: y = -tmp
    for (int i = 0; i < TILE_LENGTH; ++i) {
      yLocal.SetValue(i, static_cast<half>(0.0f));
    }
    AscendC::Sub(yLocal, yLocal, tmpLocal, TILE_LENGTH);

    outQueueZ.EnQue<half>(yLocal);
    inQueueX.FreeTensor(xLocal);
    inQueueX.FreeTensor(tmpLocal);
  }

  __aicore__ inline void CopyOut(int32_t progress) {
    AscendC::LocalTensor<half> zLocal = outQueueZ.DeQue<half>();
    AscendC::DataCopy(zGm[progress * TILE_LENGTH], zLocal, TILE_LENGTH);
    outQueueZ.FreeTensor(zLocal);
  }

private:
  AscendC::TPipe pipe;
  AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
  AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
  AscendC::GlobalTensor<half> xGm;
  AscendC::GlobalTensor<half> zGm;
};

extern "C" __global__ __aicore__ void logsigmoid_custom(GM_ADDR x, GM_ADDR z) {
  KernelLogSigmoid op;
  op.Init(x, z);
  op.Process();
}

#ifndef ASCENDC_CPU_DEBUG
void logsigmoid_custom_do(uint32_t blockDim, void *stream, uint8_t *x,
                          uint8_t *z) {
  logsigmoid_custom<<<blockDim, nullptr, stream>>>(x, z);
}
#endif
